
#pragma once

#include "mapbox/earcut.hpp"
#include <vector>
#include <array>
#include <string>

namespace mapbox
{
	namespace util
	{
// 		template<typename T> struct nth<0, T>
// 		{
// 			inline static auto get(const T& t)
// 			{
// 				return t.x;
// 			}
// 		};
// 
// 		template<typename T> struct nth<1, T>
// 		{
// 			inline static auto get(const T& t)
// 			{
// 				return t.x;
// 			}
// 		};
 	}
}

// struct Point3D
// {
// 	double x;
// 	double y;
// //	double z;
// };

//using Point3D = std::pair<double, double>;
using Point3D = std::array<double, 2>;
using PolygonData = std::vector<std::vector<Point3D> >;
using MultiPolygonData = std::vector<PolygonData>;

template<typename T, typename Polygon>
class EarcutTesselator
{
public:
	using Vertex = std::array<T, 2>;
	using Vertices = std::vector<Vertex>;

	EarcutTesselator(const Polygon & polygon) : polygon_(polygon)
	{
		for (const auto & ring : polygon)
		{
			for (const auto & vertex : ring)
			{
				vertices_.emplace_back(Vertex{{T(vertex[0]),T(vertex[1])}}
				);
			}
		}
	}

	EarcutTesselator& operator=(const EarcutTesselator&) = delete;

	void run()
	{
		indices_ = mapbox::earcut(polygon_);
	}

	std::vector<uint32_t> const& indices() const 
	{
		return indices_;
	}

	Vertices const& vertices() const
	{
		return vertices_;
	}

private:
	const Polygon&        polygon_;
	Vertices              vertices_;
	std::vector<uint32_t> indices_;
};



void parse_data(std::string const & geojson_path, MultiPolygonData & data);
void write_offfile(std::string const & offfile_path, MultiPolygonData const & data);